/**
 * author:Administrator
 * date:2016/1/6
 * desc: test account
 *
 * ------user (1-N) book ---------
 * step 1: create userA
 * step 2: create 2 books, the books belongs to userA
 * step 3: query userA, will find these two books
 */
"use strict";

var assert = require('assert');
var request = require('request');
var testUtils = require('./test_utils.js');

var httpPort = require('../config/index.js').http.port;
var __hostPort = 'http://localhost:' + httpPort;
var __user = {
  name: 'foo'
};
var __book1 = {name: 'book1'};
var __book2 = {name: 'book2'};
var __uid;

var stepCreateUser;
function waitCreateUserDone(done) {
  if (!!stepCreateUser)done();
  else setTimeout(function () {
    waitCreateUserDone(done);
  }, 500);
}

var stepCreateBook;
function waitCreateBookDone(done) {
  if (!!stepCreateBook)done();
  else setTimeout(function () {
    waitCreateBookDone(done);
  }, 500);
}

var stepRead;
function waitReadDone(done) {
  if (!!stepRead)done();
  else setTimeout(function () {
    waitReadDone(done);
  }, 500);
}

describe('user (1-N) book', function () {
  it('create user', function (done) {
    var url = __hostPort + '/users';
    testUtils.postJson(url, __user, function (err, res, data) {
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      assert.equal(0, data.errcode);
      __uid = data.user.id;
      stepCreateUser = 1;
      done();
    });
  });
});

describe('user (1-N) book', function () {
  this.timeout(5000);
  before(function (done) {
    waitCreateUserDone(done);
  });

  it('create 2 books', function (done) {
    var url = __hostPort + '/books/';
    var doneCnt = 0;
    // book1
    __book1.uid = __uid;
    testUtils.postJson(url, __book1, function (err, res, data) {
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      assert.equal(0, data.errcode);
      __book1.id = data.book.id;
      if (++doneCnt == 2) {
        stepCreateBook = 1;
        done();
      }
    });

    // book2
    __book2.uid = __uid;
    testUtils.postJson(url, __book2, function (err, res, data) {
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      assert.equal(0, data.errcode);
      __book2.id = data.book.id;
      if (++doneCnt == 2) {
        stepCreateBook = 1;
        done();
      }
    });
  });
});

describe('user (1-N) book', function () {
  this.timeout(5000);
  before(function (done) {
    waitCreateBookDone(done);
  });

  it('read user with book', function (done) {
    var url = __hostPort + '/users/' + __uid;
    request(url, function (err, res, data) {
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      assert.equal(0, data.errcode);
      assert.equal(__user.name, data.user.name);
      var books = data.user.books;
      // since books is array, we sort it
      var id1 = books[0].id;
      var id2 = books[1].id;
      var __id1 = __book1.id;
      var __id2 = __book2.id;
      assert.equal(id1 > id2 ? id1 : id2, __id1 > __id2 ? __id1 : __id2);
      assert.equal(id1 + id2, __id1 + __id2);
      done();
    });
  });
});
